package vip.xiaonuo.facedoor.modular.device.listener;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.modular.device.service.DeviceSync;


import javax.annotation.Resource;

/**
 * 设备数据同步处理中心
 */
@Service
public class DeviceHandleCenter {

    public void handle(DeviceChangeTypeEnum changeType,DeviceSourceEnum source,Object obj){
        DeviceSync sync = null;
        // 先根据source获取处理对象
        if(source == DeviceSourceEnum.AI_BOX){
            sync= SpringUtil.getBean("AiBoxDeviceSyncService.class");
        }else if(source == DeviceSourceEnum.BASE){
            sync = SpringUtil.getBean("FacedoorDeviceSyncService.class");
        }else if(source == DeviceSourceEnum.TRAILS){
            sync = SpringUtil.getBean("StarisDeviceSyncService.class");
        }

        if(sync!=null){
            if(changeType== DeviceChangeTypeEnum.ADD){
                Long id = sync.add(obj);
                if(id > 0){
                    sync.saveRelate(id,obj);
                }
            }else if(changeType== DeviceChangeTypeEnum.EDIT){
                Long id = sync.update(obj);
                if(id > 0){
                    sync.saveRelate(id,obj);
                }
            }else if(changeType== DeviceChangeTypeEnum.DELETE){
                sync.delete(obj);
            }
        }
    }

}
